<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>绑定所有函数示例</title>
    <style>
        body {
            font-family: '微软雅黑', sans-serif;
            background-color: #f0f4f8;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 400px;
            padding: 20px;
            text-align: center;
        }

        h1 {
            color: #4CAF50;
            font-size: 24px;
            margin-bottom: 20px;
        }

        p {
            font-size: 16px;
            color: #555;
            margin: 10px 0;
        }

        .button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border-radius: 4px;
            transition: background-color 0.3s;
        }

        .button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            font-size: 18px;
            font-weight: bold;
            color: #00796b;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>JavaScript 绑定所有函数示例</h1>
        <p>点击按钮查看绑定所有函数的效果：</p>
        <button class="button" id="bindButton">绑定并调用函数</button>
        <p id="result" class="result"></p>
    </div>

    <script>
        // bindAll 函数实现
        const bindAll = (obj, ...handlers) =>
            handlers.forEach(handler => {
                const h = obj[handler];
                obj[handler] = function () {
                    return h.apply(obj);
                };
            });

        // 定义一个对象，用作函数的上下文
        const person = {
            name: '小明',
            greet() {
                return `你好，${this.name}！`;
            },
            sayGoodbye() {
                return `再见，${this.name}！`;
            }
        };

        // 绑定所有函数
        bindAll(person, 'greet', 'sayGoodbye');

        // 获取按钮和显示结果的元素
        const button = document.getElementById('bindButton');
        const resultElement = document.getElementById('result');

        // 给按钮添加点击事件
        button.addEventListener('click', () => {
            // 调用绑定后的 greet 和 sayGoodbye 函数
            const greetResult = person.greet();
            const goodbyeResult = person.sayGoodbye();
            resultElement.textContent = `${greetResult} ${goodbyeResult}`;  // 显示结果
        });
    </script>
</body>

</html>